import Parse.Parser;
import com.exception.PL0Exception;

import java.io.File;
import java.util.Objects;
import java.util.Scanner;

public class MainFunc {
    public static void main(String[] args) {
        System.out.println("please place the pl0 file in the folder \"./resources/test\"");
        String path = "resources/test/";
        File file = new File(path), target;
        int No = -1;
        String[] files = null;
        while (true) {
            if (No == -1) {
                Scanner scanner = new Scanner(System.in);
                if (file.exists()) {

                    files = file.list();

                    for (int i = 0; i < Objects.requireNonNull(files).length; i++) {
                        System.out.println(i + ", " + files[i]);
                    }
                    System.out.println("please select the file with No(tap -1 to flush).");

                    No = scanner.nextInt();
                    while (No > files.length) {
                        System.out.println("please input the right No. No such file");
                        No = scanner.nextInt();
                    }
                } else {
                    System.out.println("can't find resources folder, please input the absolute path of target file.");
                    path = scanner.nextLine();
                    No = -2;
                }
                continue;
            } else if (No == -2) {
                target = new File(path);
            } else {
                target = new File(path + files[No]);
            }

            System.out.println("selected file: " + target.getAbsolutePath());
            Parser p = new Parser(target);
            try {
                p.check();
                System.out.println("output of LexAnalyser is placed in the folder \"./resources\" with name \"res_token_{your filename}.txt\"");
                System.out.println("right");
            } catch (PL0Exception e) {
                e.printMsg();
            }
            break;
        }
    }
}
